package com.hjb.springbootrocketmq;

import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.remoting.exception.RemotingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Test {
    private static final Logger logger = LoggerFactory.getLogger(Test.class);
    @Autowired
    private DefaultMQProducer defaultMQProducer;

    @GetMapping("/test")
    public void send() throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
        for (int i = 0; i < 10; i++) {
            String msg = "demo msg test" + String.valueOf(i);
            logger.info("开始发送消息：" + msg);
            Message sendMsg = new Message("DemoTopic", "DemoTag", msg.getBytes());
            //默认3秒超时
            SendResult sendResult = defaultMQProducer.send(sendMsg);
            logger.info("消息发送响应信息：" + sendResult.toString());
        }
    }
}
